IDL=99;//FLAG TO SHOW IF SELECTIONS SWITCHED ON
IDS=0;//TEMP ID STORE
LMT=14;//CURRENT NO OF ENTRIES
MW=0;//MAX WORDS IN MAN MESS
var thisdate= new Date();
var saveType = 'ud';
var loadType = 'du';//SET FOR SAVED DATA LOADING
Saved="NOT SAVED";
function letternumber(e,an) 
{ 
var key; 
var keychar;
if((an=="a")||(an=="b")){a="abcdefghijklmnopqrstuvwxyz 0123456789";}else{a="0123456789";}
if (window.event) 
key = window.event.keyCode; 
else if (e) 
key = e.which; 
else 
return true; 
if (MW>250){alert("Maximum Message Length=250 Chrs!");}
keychar = String.fromCharCode(key); 
keychar = keychar.toLowerCase(); 
// control keys 
if ((key==null) || (key==0) || (key==8) || 
(key==9) || (key==13) || (key==27) ) 
return true; 
// alphas and numbers 
else if (((a).indexOf(keychar) > -1))
{MW++; 
return true;} 
else 
return false; 
} 
function loadDoc(){ 
document.myForm.fileSrc.value="LOADED"; 
//xmlData.async = false; 
//xmlData.load("./Db/" + docSrc); 
//xmlData.load("./Db/Match67.xml");

var XmlD=phoneForm.savedData1;
	XmlD.load("savedXML1");
	XmlD.value=XmlD.getAttribute("persistedData");

	//if not the first time, check userData first
	if((XmlD.value)!="null") {
		xmlData.loadXML(XmlD.value);
		}
	else    {   xmlData.load("./Db/Match07.xml");
	        }
         if(loadType=='ud') {xmlData.load("./Db/Match07.xml");}//RESET OPTION

//xmlData.load("http://home.1asphost.com/cazmumble1/Db/Match.xml");//LOAD DATA FROM SITE
var xslData = new ActiveXObject("Microsoft.XMLDOM"); 
xslData.async = false; 
xslData.load("PYR.xsl");
//xslData.load("http://home.1asphost.com/cazmumble1/MGR.xsl");//LOAD DATA FROM SITE
xmlForm.innerHTML = xmlData.transformNode(xslData.documentElement); 
patht="/TEAM[0]/TEST[0]/LMT[0]";c=xmlData.selectSingleNode(patht).text;//Set for Demo test
LMT=parseInt(c);

} 
function showDoc(){ 
document.write(xmlForm.innerHTML);
} 
function iCheck(i){
if (i.substring(0,1)=="0"){i=i.substring(1,2);}//CHOP LEADING 0
return(i);
}
function showxml(){ 
alert(xmlData.xml);
} 
function updateCheck(path,srcObj){ 
xmlData.selectSingleNode(path).text=srcObj.checked;
}

function updateNode(path,srcObj,type,id){
//type:0=DATE 1=RESULT 9=OTHER id=ELEMENT(ID)or9 if not NAME or TITLE
i=srcObj.value;//SAVE VALUE
if (type==0){//DATE
if (i==0){return;}//DEFAULT VALUE NO ACTION
if (i.length<4){alert("Invalid-MUST BE 4 digits i.e '0403' for 4th March");
srcObj.value=xmlData.selectSingleNode(path).text;return;}//RESTORE OLD 
else{sel=String(i);dy=sel.substring(0,2);mth=sel.substring(2,4);dte=dy+"/"+mth;
if ((dy<1)||(dy>31)||(mth<1)||(mth>12)){alert("Invalid-Day must be 1-31 and Month must be 1-12");
srcObj.value=xmlData.selectSingleNode(path).text;return;}//RESTORE OLD 
patht="/TEAM[0]/HISTORY[0]/PEOPLE[0]/DATE[" +id+"]/FMT[0]";
xmlData.selectSingleNode(patht).text=dte;}//STORE FORMATTED DATE
}
if (type==1){//RESULT
patht="/TEAM[0]/TEST[0]/INF[0]";C=xmlData.selectSingleNode(patht).text;//Set for Demo test
if ((id==4)&&(C=="D")){alert("NO MORE RESULTS CAN BE ENTERED IN DEMO!! SEE REGISTRATION");return;}
patht="/TEAM[0]/TEST[0]/USE[0]";C=xmlData.selectSingleNode(patht).text;C++;xmlData.selectSingleNode(patht).text=C;//Set for Limit test
if (C>50){alert("NO MORE RESULTS CAN BE ENTERED THIS SEASON!! PLEASE RE-REGISTER");return;}
if (i==99){return;}//DEFAULT VALUE NO ACTION
if (i.length<4){alert("Invalid-MUST BE 4 digits i.e '0201' for 2 : 1 or 1005 for 10 : 5");
srcObj.value=xmlData.selectSingleNode(path).text;return;}//RESTORE OLD
else{sel=String(i);H=sel.substring(0,2);H=iCheck(H);h=parseInt(H);A=sel.substring(2,4);A=iCheck(A);a=parseInt(A);dte=h+" : "+a;
var x=confirm("Do you want to confirm the Score as " + dte + " ? NOTE MATCH DETAILS CANNOT BE ALTERED ONCE YOU PRESS OK!");
if (!x){srcObj.value=xmlData.selectSingleNode(path).text;return;}//RESTORE OLD
patht="/TEAM[0]/HISTORY[0]/PEOPLE[0]/RESULT[" +id+"]/TITLE[0]";
xmlData.selectSingleNode(patht).text=dte;}//STORE FORMATTED RESULT

//NOW SET ELEMENTS AS READ ONLY AS RESULT CONFIRMED
pathN="/TEAM[0]/HISTORY[0]/PEOPLE[0]/DATE[" +id+"]/NAME[0]";
pathT="/TEAM[0]/HISTORY[0]/PEOPLE[0]/DATE[" +id+"]/TITLE[0]";
pathF="/TEAM[0]/HISTORY[0]/PEOPLE[0]/DATE[" +id+"]/FMT[0]";
xmlData.selectSingleNode(pathN).text="XX";
xmlData.selectSingleNode(pathT).text=xmlData.selectSingleNode(pathF).text;//SHOW FORMATTED DATE
pathT="/TEAM[0]/HISTORY[0]/PEOPLE[0]/PERSON[" +id+"]/TITLE[0]";xmlData.selectSingleNode(pathT).text="XX";
pathT="/TEAM[0]/HISTORY[0]/PEOPLE[0]/VENUE[" +id+"]/TITLE[0]";xmlData.selectSingleNode(pathT).text="XX";
pathN="/TEAM[0]/HISTORY[0]/PEOPLE[0]/RESULT[" +id+"]/NAME[0]";xmlData.selectSingleNode(pathN).text="XX";

//NOW UPDATE HISTORY FIELDS IF LEAGUE GAME
patht="/TEAM[0]/HISTORY[0]/PEOPLE[0]/ICON[" +id+"]/NAME[0]";
P=xmlData.selectSingleNode(patht).text;//GET CURRENT MATCH TYPE
if (P==0){
patht="/TEAM[0]/HISTORY[0]/PEOPLE[0]/HOME[" +id+"]";
home=xmlData.selectSingleNode(patht).text;//GET CURRENT HOME
updateHistory(home,H,A);}//CALL SUB TO CALC HISTORY

//NOW SHOW NEXT RESULT ENTERABLE
id++;
if (id<7){pathT="/TEAM[0]/HISTORY[0]/PEOPLE[0]/RESULT[" +id+"]/TITLE[0]";xmlData.selectSingleNode(pathT).text="YY";}
if (id==6){Rotate();pathT="/TEAM[0]/HISTORY[0]/PEOPLE[0]/RESULT[" +id+"]/TITLE[0]";xmlData.selectSingleNode(pathT).text="";}
thisdate.setTime(thisdate.getTime());
pathN=thisdate.toGMTString();pathN=pathN.substring(0,16)+Saved;
patht="/TEAM[0]/HISTORY[0]/UPDATE[0]";
xmlData.selectSingleNode(patht).text=pathN;
transform("MGR.XSL");
return;//EXIT SO FLAGS NOT OVERWRITTEN
}

xmlData.selectSingleNode(path).text=srcObj.value; 
}

function Rotate(){
//REG ONLY ROTATES XML ELMTS
pathT="/TEAM[0]/HISTORY[0]/PEOPLE[0]/";
i=0;
while(i<10){
RotE("DATE","/NAME[0]","/TITLE[0]","/FMT[0]");
RotE("RESULT","/NAME[0]","/TITLE[0]",0);
RotE("PERSON","/NAME[0]","/TITLE[0]",0);
RotE("HOME",0,0,0);
RotE("VENUE","/NAME[0]","/TITLE[0]",0);
RotE("ICON","/NAME[0]","/SEL[0]",0);
RotE("ICON","/NAME[0]","/SEL[0]",8);
i++;}

}

function RotE(e0,e1,e2,e3){
//SWAP ONE SET

pathF=pathT + e0 + "[" + i + "]";pathN=pathT + e0 + "[" + (i+1) + "]";//get SWAP First/Next PATHS
if (e3==8){pathF=pathT + e0 + "[" + (LMT+i) + "]";pathN=pathT + e0 + "[" + (LMT+i+1) + "]";}
if (e1==0){P=xmlData.selectSingleNode(pathN).text;xmlData.selectSingleNode(pathF).text=P;return;}
pathF1=pathF + e1;pathN1=pathN + e1;
P=xmlData.selectSingleNode(pathN1).text;xmlData.selectSingleNode(pathF1).text=P;
if (e2==0){return;}//NO 2nd SWAP
pathF1=pathF + e2;pathN1=pathN + e2;//SET 2nd SUB ELEMENT PATHS
P=xmlData.selectSingleNode(pathN1).text;xmlData.selectSingleNode(pathF1).text=P;//SWAP 2nd
if (e3==8){return;}//NO 3rd SWAP
if (e3==0){return;}//NO 3rd SWAP
pathF1=pathF + e3;pathN1=pathN + e3;//SET 3rd SUB ELEMENT PATHS
P=xmlData.selectSingleNode(pathN1).text;xmlData.selectSingleNode(pathF1).text=P;//SWAP 3rd

}

function updateHistory(home,H,A){
//GET CURRENT HISTORY DATA
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/PYD[0]";P=xmlData.selectSingleNode(patht).text;
P++;xmlData.selectSingleNode(patht).text=P;//STORE PLAYED
if(home==0){//AWAY MATCH
P=parseInt(H);H=parseInt(A);A=P;}//REVERSE VARS FOR SCORE CALC:-
//UPDATE FOR/AGT XML
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/FOR[0]";P=xmlData.selectSingleNode(patht).text;
F=parseInt(P)+parseInt(H);xmlData.selectSingleNode(patht).text=F;//STORE HOME TOT
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/AGT[0]";P=xmlData.selectSingleNode(patht).text;
G=parseInt(P)+parseInt(A);xmlData.selectSingleNode(patht).text=G;//STORE AWAY TOT
//P=F-G;patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/GD[0]";
//xmlData.selectSingleNode(patht).text=P;//STORE GD
//RESULT CALC
if((H-A)==0){
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/DWN[0]";P=xmlData.selectSingleNode(patht).text;
PTS=1;}
else if(H<A){
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/LST[0]";P=xmlData.selectSingleNode(patht).text;
PTS=0;}
else {
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/WON[0]";P=xmlData.selectSingleNode(patht).text;
PTS=3;}
P++;xmlData.selectSingleNode(patht).text=P;//STORE CORRECT RESULT
patht="/TEAM[0]/HISTORY[0]/RESULTS[0]/PTS[0]";P=xmlData.selectSingleNode(patht).text;
P=parseInt(P)+PTS;xmlData.selectSingleNode(patht).text=P;//STORE POINTS
}
 
function updatePlayers(ID,sel){
if ((ID>98) && (IDL>98)){alert("Already RESET!");return;}
theImg= document.getElementsByTagName("img"); //GET IMAGE OBJECT HANDLE
if (ID<98){IDL=ID;theImg[ID].border=2;}else{theImg[IDL].border=0;}//BORDER ON/OFF SELECTED MATCH ICON
var i=0;sel=String(sel);var t="";//SET VARS CONVERT SELECTION TO STRING
while (i<LMT){ //CYCLE THRU AVAILABLE CHANGING ICONS TO MATCH CURRENT SELECTION
path="/TEAM[0]/HISTORY[0]/PEOPLE[0]/ICON[" +(LMT+i)+"]/NAME[0]";
t=t+xmlData.selectSingleNode(path).text;//BUILD NEW SEL STRING
s=sel.substring(i,i+1);
theImg[LMT+i].src="./images/Image-" + s +".gif"; //SET ACTUAL AVAIL IMAGE
xmlData.selectSingleNode(path).text=s;//SET XML AVAL ICON TEXT
i++;}
if (ID>98){//RESAVE SEL STRING AS EDITING AVAILABLE DONE
path="/TEAM[0]/HISTORY[0]/PEOPLE[0]/ICON[" +(LMT+IDL)+"]/SEL[0]";
xmlData.selectSingleNode(path).text=t;IDL=99;}
}

 
function updateImage(path,srcObj,ID,XSL){
if (ID>13){IDS=ID-14;} // GET ICON NUMBER IN RANGE
i="/TEAM[0]/HISTORY[0]/PEOPLE[0]/ICON[" +(LMT+IDS)+"]/SEL[0]";
SEL=xmlData.selectSingleNode(i).text; //GET ICON SELECTION LIST
i=xmlData.selectSingleNode(path).text;  //GET CURRENT ICON NUMBER
if (ID==IDL){updatePlayers(99,66666666666666);return;} // 2ND PRESS HIDE ICONS
if (i>5){updatePlayers(ID-14,SEL);return;} // VIEW/ALTER PLAYERS PRESSED
if (XSL==2){return;} //USR VIEW CANT EDIT RETURN
if (i<3){lt=2;st=0;}else{lt=5;st=3;}
i++;  //INCREMENT ICON NUMBER

if (ID>27){lt=4;st=0;}//FOR POSITION CHANGES ONLY
if (i>lt){i=st;}
if (ID>27){srcObj.src="./images/Pos-" + i +".gif";}else{srcObj.src="./images/Image-" + i +".gif";}
xmlData.selectSingleNode(path).text=i;     //STORE NEW ICON NO
  //CHANGE ICON DISPLAYED
} 
function updateAttribute(path,srcObj){ 
xmlData.selectSingleNode(path).text=srcObj.value; 
} 
function submitInfo(selSelectObject){ 
if (selSelectObject.options[selSelectObject.selectedIndex].value != ""){ 
var xslfile=selSelectObject.options[selSelectObject.selectedIndex].value} 
alert(xslfile); 
transform(xslfile);
} 

function transform(xslfile){
var xslData = new ActiveXObject("Microsoft.XMLDOM"); 
xslData.async = false; 
xslData.load(xslfile); 
xmlForm.innerHTML = xmlData.transformNode(xslData.documentElement);
}


function updatexml(){
if (IDL<99){alert("Cannot SAVE until Players RESET!");return;} 
var httpOb = new ActiveXObject("Microsoft.XMLHTTP");
// SAVES TO ID IN <TEST>  httpOb.Open("POST","xmlsave1.asp", false);
httpOb.Open("POST","xmlsave1.asp", false);//SAVES XML NAME
//httpOb.Open("POST","xmlsave.asp", false);//SAVES IN DB/TEST.MDB

httpOb.send(xmlData.XMLDocument);//Saves Object
//httpOb.send(xmlData.xml);//Saves xml Text
Saved="SAVED";

}

function promsg(){
  i=httpOb.readyState;
  alert(i);
}
function save() {
	var xmlD=phoneForm.savedData1;
	if (IDL<99){alert("Cannot SAVE until Players RESET!");return;}
	if(saveType=='fso') {
		//FSO Implementation
		var forWriting = 2;
		var dataFile = "c:\\swim.xml";
		var ts;
		var fso =new ActiveXObject("Scripting.FileSystemObject");
		ts = fso.OpenTextFile(dataFile,forWriting,true);
		ts.Write(xmlData.xml);
		ts.Close();
		//these lines clear the userData cache
		xmlD.value="null";
		xmlD.setAttribute("persistedData",xmlD.value);
		xmlD.save("savedXML1");
	}
	else {
		//userData implementation is the default mechanism
		xmlD.value = xmlData.xml;
//		alert (xmlD.value);
		xmlD.setAttribute("persistedData",xmlD.value);
		xmlD.save("savedXML1");
		Saved="SAVED";
	}
}

function loadData() {
	alert("in loadData");
	var XmlD=phoneForm.savedData;
	XmlD.load("savedXML");
	XmlD.value=XmlD.getAttribute("persistedData");
	//if not the first time, check userData first
	if((XmlD.value)!="null") {
		data.loadXML(XmlD.value);
	}
	else {
	//then check for the file
		var dataFile = "c:\\swim.xml";
		var forReading = 1;
		var ts, s;
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		if(fso.fileExists(dataFile)) {
			ts = fso.OpenTextFile(dataFile,forReading);
			s = ts.readAll();
			XmlD.value = s;
			alert(XmlD.value);
			xmlData.load(XmlD.value);
		}
		else {
			//If we made it here, this is the first view
			//Do Nothing, file loaded from server
			return;
		}
	}
}
